/**
 * 
 */
package org.cuatrovientos.erp.controller;



import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.apache.log4j.Logger;
import org.cuatrovientos.erp.business.DepartmentBOInterface;
import org.cuatrovientos.erp.model.Department;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.beans.factory.annotation.Autowired;



/**
 * Controller class form Department management
 * @author Natalia Bernarte
 */
@Controller
public class DepartmentController {

	// Get log4j logger
	private static final Logger logger = Logger.getLogger(DepartmentController.class);

	@Autowired
    private DepartmentBOInterface departmentBO;
    
        /**
         * default constructor
         */
        public DepartmentController () {
               logger.info("Department BEAN instantiated");
                
        }
        
        
        
        /**
         * handles default /department or /department/list request
         * @param model
         * @return the name of the view to show
         *  @RequestMapping({"/department","/department/list"})
         */
        @RequestMapping({"/department","*","/department/list"})
        public ModelAndView departmentDefault() {
        	logger.info("default");
 
                Map<String, Object> model = new HashMap<String, Object>();

                List<Department> departmentList = departmentBO.selectAll();
                model.put("departments", departmentList );
                return new ModelAndView("DepartmentList", model);     
        }

        /**
         * handles /department/search request, redirected to default page
         * @param model
         * @return the name of the view to show
         *  @RequestMapping({"/department/search"})
         */
        @RequestMapping(method=RequestMethod.POST,value={"/department/search"})
        public ModelAndView search(@RequestParam(value="description", defaultValue="", required=true) String description) {
        	logger.info("search");
 
            Map<String, Object> model = new HashMap<String, Object>();

            List<Department> departmentList = departmentBO.search(description);
            model.put("department", departmentList );
            
            return new ModelAndView("departmentList", model);     
        }

        /**
         * handles delete department, and redirect to department default
         * @param model
         * @return the name of the view to show
         *  @RequestMapping({"/department","/department/list"})
         */
        @RequestMapping(method=RequestMethod.GET,value={"/department/delete"})
        public String deleteDepartment(@RequestParam(value="id", defaultValue="", required=true) int id) {
        	logger.info("delete " + id);

            departmentBO.deleteById(id);
            return "redirect:/department";

        }
        
        /**
         * handles new department request. In the jsp we are using spring's form:form tags
         * Spring expects a model called by default 'command' or any name
         * we set to it: in this case "department"
         * @return the name of the view to show
         * @RequestMapping({"/department/new"})
         */
        @RequestMapping(method=RequestMethod.GET,value={"/department/new"})
        public ModelAndView newDepartment() {
        		logger.info("new department ");
                Department department = new Department();
               
                return new ModelAndView("NewDepartment", "department", department);
        }
        
        /**
         * handles new department request. In the jsp we are using spring's form:form tags
         * Spring expects a model called by default 'command' or any name
         * we set to it.
         * @return the name of the view to show
         * @RequestMapping({"/department/new"})
         */
        @RequestMapping(method=RequestMethod.POST,value={"/department/save"})
        public String newDepartment(@Valid Department department, BindingResult bindingResult) {
        		logger.info("new department " + department.toString());
        		
        		// In case of validation errors, go back to form
        		if (bindingResult.hasErrors()) {
        			Map<String, Object> model = new HashMap<String, Object>();
        			model.put("department", department);
        			return "NewDepartment";
        		}
        		
        		// If data is ok, insert and go on
                departmentBO.insert(department);
                return "redirect:/department";
        }
        
        /**
         * handles delete department, and redirect to department default
         * @param model
         * @return the name of the view to show
         *  @RequestMapping({"/department","/department/list"})
         */
        @RequestMapping(method=RequestMethod.GET,value={"/department/detail"})
        public ModelAndView departmentDetail(@RequestParam(value="id", defaultValue="", required=true) int id) {
        	logger.info("Detail " + id);
            Map<String,Object> model = new HashMap<String,Object>();
            Department department = departmentBO.selectById(id);
            model.put("department", department);
            
            return new ModelAndView("DepartmentDetail",model);

        }
        
        /**
         * handles department update, first part: register loaded
         * @param model
         * @return the name of the view to show
         *  @RequestMapping({"/department/update"})
         */
        @RequestMapping(method=RequestMethod.GET,value={"/department/update"})
        public ModelAndView update(@RequestParam(value="id", defaultValue="", required=true) int id) {
        	logger.info("update " + id);
                Map<String,Object> model = new HashMap<String,Object>();
                Department department = departmentBO.selectById(id);
                model.put("department", department);
               return new ModelAndView("UpdateDepartment",model);

        }
        
        /**
         * handles save update department request. In the jsp we are using spring's form:form tags
         * Spring expects a model called by default 'command' or any name
         * we set to it.
         * @return the name of the view to show
         * @RequestMapping({"/department/saveUpdate"})
         */
        @RequestMapping(method=RequestMethod.POST,value={"/shipment/saveupdate"})
        public String saveUpdate(@Valid Department department, BindingResult bindingResult) {
        		logger.info("update department " + department.toString());
        		
        		// In case of validation errors, go back to form
        		if (bindingResult.hasErrors()) {
        			Map<String, Object> model = new HashMap<String, Object>();
        			model.put("department", department);
        			return "UpdateDepartment";
        		}
        		
                departmentBO.update(department);
                return "redirect:/department";
        }
}
